<?php
header("Content-Type:text/html;charset=utf-8");
$userAgent  = 'Mozilla/5.0 (iPhone; U; CPU iPhone OS 3_0 like Mac OS X; en-us) AppleWebKit/528.18 (KHTML, like Gecko) Version/4.0 Mobile/7A341 Safari/528.16';

function re($code=0,$msg=null){
    return array('code' => $code, 'msg' =>'登陆错误：<br/>'.$msg);
}

//获取登陆地址
function getLoginUrl(){
    global $userAgent;
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, "http://pt.3g.qq.com/");
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); //这行是设定curl是否跟随header发送的location, 重要
    curl_setopt($ch, CURLOPT_POST, 0);
    curl_setopt($ch, CURLOPT_REFERER, 'http://3g.qq.com/');
    curl_setopt($ch, CURLOPT_USERAGENT, $userAgent);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);

    $return = curl_exec($ch);

    curl_close($ch);
    preg_match('/action=".*?"/', $return,$re);
    return str_replace(array('action="','"'), '', $re[0]);;
}

//登陆3gqq官网
function loginIndexPage($qq='',$pwd='',$loginType=2){
    global $userAgent;
    $loginUrl   = getLoginUrl();
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $loginUrl);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); //这行是设定curl是否跟随header发送的location, 重要
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_HEADER, false);
    curl_setopt($ch, CURLOPT_REFERER, 'http://pt.3g.qq.com/');
    curl_setopt($ch, CURLOPT_USERAGENT, $userAgent);
    //curl_setopt($ch, "Connection", "Keep-Alive");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, "qq=$qq&pwd=$pwd&bid=1&loginType=$loginType&login_url=$loginUrl&loginsubmit=登陆&q_from=&loginTitle=");

    $return = curl_exec($ch);
    $header = curl_getinfo($ch);
    $redirect_count = $header['redirect_count'];
    curl_close($ch);
    if($redirect_count == 0) {
        if(preg_match('/登录密码错误/', $return)) {
            return re(0, '登陆密码错误');
        }elseif(preg_match('/系统检测到您的操作异常，为保证您的号码安全，请输入验证码进行验证，防止他人盗取密码/', $return)) {
            return re(0, '1、请登陆手机腾讯网解除异常。<br/>2、使用服务器所在省份ip登陆QQ。<br/>3、可正常到本站挂QQ。');
        }else{
			 preg_match('~<div id="gaia_loginbox" class="body">(.*?)</div>~s',$return, $show);
             return re(0, $show[1]);
        }
    }else{
        return re(1, $header['url']);
    }
}
//登陆qq
function loginQQ($qq=null, $pwd=null, $loginType=2){
    global $userAgent;
    $url    = loginIndexPage($qq, $pwd, $loginType);
    if($url['code'] ==0) {
        return $url['msg'];
    }
    
    preg_match('/sid=(.*?)&aid/', $url['msg'], $matches);
    $sid    = $matches[1];
    $url    = 'http://pt5.3g.qq.com/s?aid=nLogin3gqq&auto=1&s_it=1&g_f=1796&sid='.$sid;
    
    $loginUrl   = getLoginUrl();
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, "http://pt.3g.qq.com/handleLogin?r=".  rand(20000000, 99999999999));
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); //这行是设定curl是否跟随header发送的location, 重要
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_HEADER, false);
    curl_setopt($ch, CURLOPT_REFERER, $url);
    //curl_setopt($ch, CURLOPT_REFERER, 'http://pt5.3g.qq.com/s?aid=nLogin3gqq&auto=1&s_it=1&g_f=1796&sid='.$sid);
    curl_setopt($ch, CURLOPT_USERAGENT, $userAgent);
    //curl_setopt($ch, "Connection", "Keep-Alive");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, "aid=nLoginHandle&auto=1&bid_code=3GQQ&loginType=$loginType&login_url=http://pt5.3g.qq.com/s?aid=nLoginnew&q_from=3GQQ&modifySKey=0&q_from=&pwd=&qq=$qq&sid=$sid&toQQchat=true");

    $return = curl_exec($ch);
    $header = curl_getinfo($ch);
    $redirect_count = $header['redirect_count'];
    curl_close($ch);
    if ($redirect_count ==1 ){
        return $header['url'];
    }else{
        return 'error';
    }
}

if(!empty($_GET['qq']) && !empty ($_GET['pwd']) && !empty($_GET['loginType'])) {
    echo "服务器IP:".$_SERVER['SERVER_ADDR'].'<br/>';
    echo loginQQ($_GET['qq'], $_GET['pwd'], $_GET['loginType']);
}else{
    echo "缺少QQ号码、密码或者登陆类型";
}
?>